﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Haha.Mvc
{
    public class Main
    {
        private HttpContext Conext;
        private HttpRequest Request;
        private HttpResponse Response;

        public void Run(HttpContext context, string url)
        {
#if DEBUG
            RunDebug(context, url);
#else
            RunRelease(context, url);
#endif
        }
        private void RunDebug(HttpContext context, string url)
        {
            
                this.Conext = context;
                this.Request = context.Request;
                this.Response = context.Response;


                System.Text.StringBuilder urlWithOutAppAndExt = new System.Text.StringBuilder();
                NameValueCollection get = new NameValueCollection();
                string root = MvcInit.New.Cfg["root"].ToString().ToLower();
                if (root.Length > 0 && url.ToLower().StartsWith(root))
                {
                    url = url.Substring(root.Length);
                }
                List<string> tmps = url.Split('/').Where(d => d.Length > 0).ToList();
                string action = "";
                string method = "";
                if (tmps.Count == 0)
                {
                    Out("无效URL!" + url, url);
                    return;
                }

                if (MvcInit.ExitsApp(tmps[0]))
                {
                    if (tmps.Count == 1)
                    {
                        Out("无效URL!" + url, url);
                        return;
                    }
                    action = "/" + tmps[0];
                    tmps.RemoveAt(0);
                }
                var tmp = tmps[0];
                if (tmp.Contains("!"))
                {
                    var tmp2 = tmp.Split('!');
                    action += "/" + tmp2[0];
                    method = tmp2[1];
                }
                else if (tmps.Count == 1)
                {
                    Out("无效URL!" + url, url);
                    return;
                }
                else
                {
                    action += "/" + tmp;
                    method = tmps[1];
                }
                if (method.Contains("."))
                {
                    method = method.Substring(0, method.LastIndexOf("."));
                }
                //context.Response.Write("url" + url);
                ActionMethodInfo data = MvcInit.New.Parse(context, action, method);

                Write(data);

            

        }

        private void RunRelease(HttpContext context, string url)
        {
            try
            {
                this.Conext = context;
                this.Request = context.Request;
                this.Response = context.Response;
               
         
                System.Text.StringBuilder urlWithOutAppAndExt = new System.Text.StringBuilder();
                NameValueCollection get = new NameValueCollection();
                string root = MvcInit.New.Cfg["root"].ToString().ToLower();
                if (root.Length > 0 && url.ToLower().StartsWith(root))
                {
                    url = url.Substring(root.Length);
                }
                List<string> tmps = url.Split('/').Where(d => d.Length > 0).ToList();
                string action = "";
                string method = "";
                if (tmps.Count == 0)
                {
                    Out("无效URL!" + url, url);
                    return;
                }

                if (MvcInit.ExitsApp(tmps[0]))
                {
                    if (tmps.Count == 1)
                    {
                        Out("无效URL!" + url, url);
                        return;
                    }
                    action = "/" + tmps[0];
                    tmps.RemoveAt(0);
                }
                var tmp = tmps[0];
                if (tmp.Contains("!"))
                {
                    var tmp2 = tmp.Split('!');
                    action += "/" + tmp2[0];
                    method = tmp2[1];
                }
                else if (tmps.Count == 1)
                {
                    Out("无效URL!" + url, url);
                    return;
                }
                else
                {
                    action += "/" + tmp;
                    method = tmps[1];
                }
                if (method.Contains("."))
                {
                    method = method.Substring(0, method.LastIndexOf("."));
                }
                //context.Response.Write("url" + url);
                ActionMethodInfo data = MvcInit.New.Parse(context, action, method);

                Write(data);
               
            }
            catch (Exception ex)
            {
                Out(ex.Message, "");
            }
               
        }

        private void Out(string p,string url)
        {
            //Response.WriteFile(url);
            Response.Write(p);
        }

        private void Write(ActionMethodInfo p)
        {
            var data = p.Restult;
            if (data is MvcDown)
            {
                MvcDown ms = data as MvcDown;
                Response.Clear();
                byte[] buff = ms.Data;
                Response.OutputStream.Write(buff, 0, buff.Length);
                Response.ContentType = "application/octet-stream;charset=UTF-8";
                String name = ms.FileName;
                Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
                return;
            }
            if (data is MvcImage)
            {
                MvcImage ms = data as MvcImage;
                Response.Clear();
                byte[] buff = ms.Data;
                Response.OutputStream.Write(buff, 0, buff.Length);
                Response.ContentType = p.ContextType;
                return;
            }
                Response.Write(data);
         
        }
    }
}
